// pages/main/main.js
const app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:'https://mp.weixin.qq.com/'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that =this;
      that.setData({
          baseUrl: app.globalData.baseUrl
        })
        wx.request({
          url: app.globalData.baseUrl + '/api/index/getCishanList',
          data: {
            is_hot: 1
          },
          header: {
            'content-type': 'application/json'
          },
          success: res => {
            console.log(res.data)
            that.setData({
              hot: res.data.data[0].id
            })
          }
        })

  },
  uploadImgApi: function (e) {
    console.log(e);
    var that = this;
    wx.downloadFile({
      url:e, 
      success (res) {
        console.log(res)
        if (res.statusCode === 200) {
        wx.uploadFile({
            url: app.globalData.baseUrl + '/api/index/uploadImgApi',
            filePath: res.tempFilePath,
            name: 'img',
            formData:({
            }),
        success: function (res) {
          console.log(res)
           var arr = JSON.parse(res.data);

          wx.setStorageSync("realHeadimg", arr.url);
        },
        complete:function(res){
            console.log(res);
        }
        })


        }
      }
    })

},
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    var that= this;
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        console.log(res)
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })

        wx.setStorageSync("userInfo", res.userInfo);
        var rawData = res.rawData;
        var signature = res.signature;
        var encryptData = res.encryptData;
       // var encryptedData = encodeURIComponent(res.encryptedData);
        var encryptedData = res.encryptedData;
        var iv = res.iv;
        wx.setStorageSync("iv", iv);
        wx.setStorageSync("encryptedData", encryptedData);
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            console.log(res.code);
            wx.request({
              url: app.globalData.baseUrl  + '/api/index/jscode2session',
              data: {
                code: res.code,
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" // 默认值
              },
              method: 'POST',
              success(res) {
                var res = res.data;
                console.log(res);
                if (res.status == 1) {
                  that.setData({
                    needAuth:0
                  })
                  wx.setStorage({
                    key: 'token',
                    data: res.data.session_key
                  });
                  wx.setStorage({
                    key: 'openid',
                    data: res.data.openid
                  });
                  wx.setStorageSync("session_key", res.data.session_key);//存到缓存里面openid
                  wx.setStorageSync("openid", res.data.openid);//存到缓存里面openid

                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000
                  })
                }

              }
            })
          }
        })
      },
      fail: function() {
          wx.showModal({
              title: "获取信息失败",
              content: "请允许授权以便为您提供给服务",
              success: function(e) {
                  e.confirm && this.getUserProfile();
              }
          });
      }
    })
  },
  bindcontribution() {
    var that = this;
    var data = this.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/contribution/contribution?status=1&bnStatus=1' 
     })
    }
  },
  bindcontribution1() {
    var that = this;
    var data = this.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/contribution/contribution?status=1&bnStatus=2' 
     })
    }
  },
  binddonation() {
    var that = this;
    var data = this.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/donation/donation' 
     })
    }
  },
  bindDonDetail() {
    var that = this;
    var data = that.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
  
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/donateDetail/donateDetail?status=4' 
     })
    }
  },
  bindHelp() {
    var that = this;
    var data = this.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
  
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/help/help'
     })
    }
  },
  bindStep() {
    var that = this;
    var data = this.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
  
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/step/step'
     })
    }
  },
  bindNeed() {
    var that = this;
    var data = this.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
  
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/need/need'
     })
    }
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var openid = wx.getStorageSync("openid");
    console.log(openid)
    if (openid!='' || openid == 'undefined') {
      this.setData({
        needAuth: 0
      })
    }else{
      this.setData({
        needAuth: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: '和善龙江',
      path:'/pages/main/main',
       imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg'
    }
  },
  onShareTimeline: function () {
    var that = this;
    return {
      title: '和善龙江',
      path:'/pages/main/main',
       imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg'
    }
  }


  
})